<?php

namespace App\Http\Controllers;

use App\Good;
use App\GoodColor;
use App\GoodInput;
use App\GoodOrder;
use App\GoodRecordInput;
use App\GoodReturn;
use App\GoodSale;
use App\GoodStackRecord;
use App\GoodStoreMember;
use App\GoodWholeSaleReturnRecord;
use Illuminate\Http\Request;

use App\Http\Requests;
use Validator;
class GoodRecordController extends Controller
{
    //
    public function collect(Request $request) {
        $validator = Validator::make($request->all(), array(
            'userId' => 'required|max:255',
            'sessionId' => 'required|max:255',
            'start' => 'required',
            'end' => 'required',
            'type' => 'required|integer',
            'perPage' => 'required'
        ));
        if ($validator->fails()) {
            $this->validatorError($validator);
            return ;
        }
        if (!$this->sessionValidate($request->sessionId))
            return ;
        $member = GoodStoreMember::where('memberId', $request->userId)->first();
        if ($request->memberId != null) {
            if ($request->type == 0) {
                $records = GoodRecordInput::where('operatorId', $request->memberId)
                    ->whereBetween('date', [$request->start, $request->end])
                    ->paginate($request->perPage);
                $pageData = [
                    "count" => $records->count(),
                    "currentPage" => $records->currentPage(),
                    "lastPage" => $records->lastPage()
                ];
                $data = [];
                foreach ($records as $record) {
                    array_push($data,[
                        "date" => $record->date,
                        'goodName' => $record->goodName,
                        'inputNumber' => $record->inputNumber,
                        'returnNumber' => $record->returnNumber,
                        'stockNumber' => $record->stockNumber,
                        'provider' => $record->provider,
                        'operatorId' => $record->operatorId,
                        'recordId' => $record->id
                    ]);
                }
                $this->returnEcho(200, '', ['pageData' => $pageData, 'list' => $data]);
                return;
            }
            else if ($request->type == 1) {
                $records = GoodWholeSaleReturnRecord::where('operatorId', $request->memberId)
                    ->whereBetween('date', [$request->start, $request->end])
                    ->paginate($request->perPage);
                $pageData = [
                    "count" => $records->count(),
                    "currentPage" => $records->currentPage(),
                    "lastPage" => $records->lastPage()
                ];
                $data = [];
                foreach ($records as $record) {
                    array_push($data,[
                        "date" => $record->date,
                        'goodName' => $record->goodName,
                        'inputNumber' => $record->inputNumber,
                        'buyer' => $record->buyer,
                        'operatorId' => $record->operatorId,
                        'recordId' => $record->id
                    ]);
                }
                $this->returnEcho(200, '', ['pageData' => $pageData, 'list' => $data]);
                return;
            }
            else if ($request->type == 2) {
                $records = GoodStackRecord::where('operatorId', $request->memberId)
                    ->whereBetween('date', [$request->start, $request->end])
                    ->paginate($request->perPage);
                $pageData = [
                    "count" => $records->count(),
                    "currentPage" => $records->currentPage(),
                    "lastPage" => $records->lastPage()
                ];
                $data = [];
                foreach ($records as $record) {
                    array_push($data,[
                        "date" => $record->date,
                        'goodName' => $record->goodName,
                        'upNumber' => $record->upNumber,
                        'downNumber' => $record->downNumber,
                        'stockNumber' => $record->stockNumber,
                        'operatorId' => $record->operatorId,
                        'recordId' => $record->id
                    ]);
                }
                $this->returnEcho(200, '', ['pageData' => $pageData, 'list' => $data]);
                return;
            }
        } else  {
            if ($request->type == 0) {
                $records = GoodRecordInput::where('storeId', $member->storeId)
                    ->whereBetween('date', [$request->start, $request->end])
                    ->paginate($request->perPage);
                $pageData = [
                    "count" => $records->count(),
                    "currentPage" => $records->currentPage(),
                    "lastPage" => $records->lastPage()
                ];
                $data = [];
                foreach ($records as $record) {
                    array_push($data,[
                        "date" => $record->date,
                        'goodName' => $record->goodName,
                        'inputNumber' => $record->inputNumber,
                        'returnNumber' => $record->returnNumber,
                        'stockNumber' => $record->stockNumber,
                        'provider' => $record->provider,
                        'operatorId' => $record->operatorId,
                        'recordId' => $record->id
                    ]);
                }
                $this->returnEcho(200, '', ['pageData' => $pageData, 'list' => $data]);
                return;
            }
            else if ($request->type == 1) {
                $records = GoodWholeSaleReturnRecord::where('storeId', $member->storeId)
                    ->whereBetween('date', [$request->start, $request->end])
                    ->paginate($request->perPage);
                $pageData = [
                    "count" => $records->count(),
                    "currentPage" => $records->currentPage(),
                    "lastPage" => $records->lastPage()
                ];
                $data = [];
                foreach ($records as $record) {
                    array_push($data,[
                        "date" => $record->date,
                        'goodName' => $record->goodName,
                        'inputNumber' => $record->inputNumber,
                        'buyer' => $record->buyer,
                        'operatorId' => $record->operatorId,
                        'recordId' => $record->id
                    ]);
                }
                $this->returnEcho(200, '', ['pageData' => $pageData, 'list' => $data]);
                return;
            }
            else if ($request->type == 2) {
                $records = GoodStackRecord::where('storeId', $member->storeId)
                    ->whereBetween('date', [$request->start, $request->end])
                    ->paginate($request->perPage);
                $pageData = [
                    "count" => $records->count(),
                    "currentPage" => $records->currentPage(),
                    "lastPage" => $records->lastPage()
                ];
                $data = [];
                foreach ($records as $record) {
                    array_push($data,[
                        "date" => $record->date,
                        'goodName' => $record->goodName,
                        'upNumber' => $record->upNumber,
                        'downNumber' => $record->downNumber,
                        'stockNumber' => $record->stockNumber,
                        'operatorId' => $record->operatorId,
                        'recordId' => $record->id
                    ]);
                }
                $this->returnEcho(200, '', ['pageData' => $pageData, 'list' => $data]);
                return;
            }
        }
    }

    public function getRecordDetail(Request $request) {
        $validator = Validator::make($request->all(), array(
            'userId' => 'required|max:255',
            'sessionId' => 'required|max:255',
            'type' => 'required|integer',
            'recordId' => 'required'
        ));
        if ($validator->fails()) {
            $this->validatorError($validator);
            return ;
        }
        if (!$this->sessionValidate($request->sessionId))
            return ;
        $member = GoodStoreMember::where('memberId', $request->userId)->first();

        if ($request->type == 0)
        {
            $input = GoodInput::where('recordId', $request->recordId);


        }



    }

    public function getSold(Request $request) {
        $validator = Validator::make($request->all(), array(
            'userId' => 'required|max:255',
            'sessionId' => 'required|max:255',
            'classId' => 'required|integer',
        ));
        if ($validator->fails()) {
            $this->validatorError($validator);
            return ;
        }
        if (!$this->sessionValidate($request->sessionId))
            return ;

        $member = GoodStoreMember::where('memberId', $request->userId)->first();

        $data = [];
        $goods = Good::where('classId', $request->classId)->get();
        foreach ($goods as $good) {
            $records = GoodSale::where('storeId', $member->storeId)
                ->where('goodId', $good->id)
                ->get();
            if ($records != null) {
                $color = GoodColor::where('goodId', $good->id)->first();
                $picture = '';
                if ($color != null)
                    $picture = $color->picture;
                array_push($data, [
                    "id" => $good->id,
                    'name' => $good->name,
                    'no'=> $good->no,
                    'picture' => $picture
                ]);
            }
        }
        $this->returnEcho(200, '', ['list' => $data]);
    }

    public function getSoldRecord(Request $request) {
        $validator = Validator::make($request->all(), array(
            'userId' => 'required|max:255',
            'sessionId' => 'required|max:255',
            'start' => 'required',
            'end' => 'required',
            'goodId' => 'required|integer',
        ));
        if ($validator->fails()) {
            $this->validatorError($validator);
            return ;
        }
        if (!$this->sessionValidate($request->sessionId))
            return ;

        $member = GoodStoreMember::where('memberId', $request->userId)->first();

        $records= GoodSale::where('storeId', $member->storeId)
            ->where('goodId', $request->goodId)
            ->whereBetween('date', [$request->start, $request->end])
            ->orderBy('id')
            ->get();

        $data = [];
        foreach ($records as $record) {
            if (!array_key_exists($record->colorName, $data)) {
                $data[$record->colorName] = [
                    'number' => $record->number,
                    'price' => $record->price * $record->number,
                    $record->sizeName => [
                        'number' => $record->number,
                        'price' => $record->price * $record->number,
                        $record->description => $record->number
                    ],
                    $record->description => $record->number
                ];
            } else {
                if (!array_key_exists($record->sizeName, $data[$record->colorName])) {
                    $data[$record->colorName]['number'] += $record->number;
                    $data[$record->colorName]['price'] += $record->price * $record->number;
                    $data[$record->colorName][$record->sizeName] = [
                        'number' => $record->number,
                        'price' => $record->price * $record->number,
                        $record->description => $record->number,
                    ];
                    if (!array_key_exists($record->description, $data[$record->colorName])) {
                        $data[$record->colorName][$record->description] = $record->number;
                    } else {
                        $data[$record->colorName][$record->description] += $record->number;
                    }
                } else {
                    $data[$record->colorName]['number'] += $record->number;
                    $data[$record->colorName]['price'] += $record->price * $record->number;
                    $data[$record->colorName][$record->sizeName]['number'] += $record->number;
                    $data[$record->colorName][$record->sizeName]['price'] += $record->price * $record->number;
                    if (!array_key_exists($record->description, $data[$record->colorName])) {
                        $data[$record->colorName][$record->description] = $record->number;
                    } else {
                        $data[$record->colorName][$record->description] += $record->number;
                    }
                    if (!array_key_exists($record->desciption, $data[$record->colorName][$record->sizeName]))
                        $data[$record->colorName][$record->sizeName][$record->description] = $record->number;
                    else
                        $data[$record->colorName][$record->sizeName][$record->description] += $record->number;
                }
            }
        }

        $result = [];
        foreach ($data as $key => $value) {
            $sizeData = [];
            $des = "";
            foreach ($value as $k => $v) {
                if ($v["number"] != null) {
                    $description = "";
                    foreach ($v as $d => $n) {
                        if ($d != "number" && $d != "price")
                        $description .= $d . " : ". $n . "\n";
                    }
                    array_push($sizeData, [
                        "size" => $k,
                        "number" => $v["number"],
                        "price" => $v["price"],
                        "description" => substr($description, 0, -1)
                    ]);
                }
                else if ($k != "number" && $k != "price") {
                    $des.= $k . " : " .$v ."\n";
                }
            }

            array_push($result, [
                "color" => $key,
                "number" => $value["number"],
                "price" => $value["price"],
                "sizeData" => $sizeData,
                "description" => substr($des, 0, -1)
            ]);
        }

/*
        $data = [];
        foreach ($mode1color as $key => $value) {
            $sizeD = array();
            foreach ($value as $size => $sizeData) {
                if ($size != "number" && $size != "price") {
                    array_push($sizeD, [
                        "size" => $size,
                        "number" => $sizeData["number"],
                        "price" => $sizeData["price"]
                    ]);
                }
            }
            array_push($data, [
                "color" => $key,
                "number" => $value["number"],
                "price" => $value["price"],
                "sizeData" => $sizeD
            ]);
        }
*/
        $this->returnEcho(200, '', $result);
        return;
    }
}
